Blog

Maria Jose Herrera

August 13, 2024

Spread the word


Share your thoughts

A Powerful addition to BoxLang’s Java interoperability!

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

BoxLang

Java Method References

In CFML, you can grab a reference to a UDF or closure, pass it around, and invoke it. BoxLang extends this to Java methods, both instance and static. Check this out:

boxlangCopy code
myInstance = new myClass();
myInstanceMethod = myInstance.myMethod;
myInstanceMethod();

And for static methods:

boxlangCopy code
myStaticUDF = src.test.java.TestCases.phase3.StaticTest::sayHello;
myStaticUDF();

Treating Java Methods as Objects

In BoxLang, Java methods (both instance and static) are objects you can pass around, invoke, and use in higher-order functions. When you reference a method on a Java class without parentheses, you get a special Function instance. This lets you treat the method like any other function.

Try BoxLang

Static Method Reference Example

Capture the static valueOf() method from the Java String class and invoke it:

boxlangCopy code
import java:java.lang.String;
javaStaticMethod = java.lang.String::valueOf;
result = javaStaticMethod("test"); // New string of "test"

Instance Method Reference Example

Capture the toUpperCase method from a String instance:

boxlangCopy code
javaInstanceMethod = "my string".toUpperCase;
result = javaInstanceMethod(); // "MY STRING"

Using Java Methods with Higher-Order Functions

BoxLang lets you use Java methods directly in place of UDFs or closures for higher-order functions. For example, sort an array in reverse order using Java’s Collections.reverseOrder().compare method:

boxlangCopy code
import java.util.Collections;
[1, 7, 3, 99, 0].sort(Collections.reverseOrder().compare); // [99, 7, 3, 1, 0]

Limitations and Considerations

There are some limitations. Many Java methods can’t be used with CF/BL’s reduceeachmap higher-order built-in functions (BIFs) because they strictly enforce the number of arguments. For example:

boxlangCopy code
import java:java.lang.Math;
[1, 2.4, 3.9, 4.5].map(Math::floor); // Error because floor only accepts a single arg!

The Math.floor() method accepts a single argument, but arrayMap() passes three arguments, causing an error.

Two-Way Compatibility with Java

BoxLang’s ability to pass closures into Java methods that accept functional interfaces or lambdas enhances two-way compatibility between Java and BoxLang. Check out this example of using a Java Stream with a BoxLang lambda:

boxlangCopy code
fruits = ["apple", "banana", "cherry", "ananas", "elderberry"];
result = fruits.stream()
  .filter(fruit -> fruit.startsWith("a"))
  .toList();

Here, we convert a BoxLang array into a Java Stream, use the Java filter() method with a BoxLang lambda, and collect the results into a list.

Conclusion

With BoxLang’s new support for Java method references and higher-order functions, Java interop just got a whole lot cooler. This feature lets you write cleaner, more powerful code by leveraging Java’s capabilities within BoxLang’s modern syntax. Give it a try and see how it can boost your development experience!

Try BoxLang

Add Your Comment

Recent Entries

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management


Opting for in-house database management involves more than just a salary. Here are some often-overlooked costs associated with maintaining your own DBA team.



1. High Salaries and Benefits


Hiring skilled DBAs is expensive. According to industry reports, the average salary of a DBA in the U.S. can range from $85,000 to over $130,000 per year, depending on experience and expertise. When you add ...

Cristobal Escobar
Cristobal Escobar
November 20, 2024
5 Signs It’s Time to Modernize Your ColdFusion / CFML Application

5 Signs It’s Time to Modernize Your ColdFusion / CFML Application

ColdFusion has long been a reliable platform for building web applications, but like any technology, it requires maintenance and modernization over time. Whether you're using Lucee or Adobe ColdFusion, it’s critical to recognize the signs that your application is no longer meeting today’s standards in performance, security, and scalability. Let’s explore five clear indicators that it’s time to modernize your ColdFusion application and how ColdFusion consulting can help breathe new life into y...

Cristobal Escobar
Cristobal Escobar
November 19, 2024
ColdBox Free Tip 5 - Building Named Routes with a Struct

ColdBox Free Tip 5 - Building Named Routes with a Struct

**Did you know ColdBox provides flexible ways to build routes using structs?** In this tip, we’ll cover how to use the `event.buildLink()` and `event.route()` methods for named routes, a feature that’s especially handy when working with dynamic URLs.

Maria Jose Herrera
Maria Jose Herrera
November 19, 2024